package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.OrderDao;
import com.delivery.www.po.Order;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public int addOrder(int userId, int storeId, int dishId, String status, Float money) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {userId, storeId, dishId, status, money};
        String sql = "insert into `order` (user_id,store_id,dish_id,status,money) values(?,?,?,?,?)";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        if (excute > 0) {
            Object[] objects = {storeId};
            String s = "update store set unprocess_order_num = unprocess_order_num+1 where store_id = ?";
            JdbcUtils.excute(connection, pstm, s, objects);
        }
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public List<Order> getCompleteOrder(int storeId, String status) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {storeId, status};
        List<Order> orders = new ArrayList<>();
        String sql = "select u.user_name, o.order_id, dish_id, money from `order` o INNER join user u on o.user_id = u.user_id where o.store_id = ? and o.status =? ";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        try {
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setUserName(rs.getString("user_name"));
                order.setDishId(rs.getInt("dish_id"));
                order.setMoney(rs.getFloat("money"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return orders;
    }

    @Override
    public Float earnings(int storeId, String status) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {storeId, status};
        String sql = "select sum(money) from `order` where store_id = ? and `status` = ?";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        Float f = null;
        try {
            if (rs.next()) {
                f = rs.getFloat("sum(money)");
                JdbcUtils.closeResource(connection, pstm, rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return f;
    }

    @Override
    public List<Order> getUnCompleteOrder(int storeId, String status) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {storeId, status};
        List<Order> orders = new ArrayList<>();
        String sql = "select u.user_name, o.order_id, dish_id, status, money from `order` o INNER join user u on o.user_id = u.user_id where o.store_id = ? and o.status !=? ";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        try {
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setUserName(rs.getString("user_name"));
                order.setDishId(rs.getInt("dish_id"));
                order.setStatus(rs.getString("status"));
                order.setMoney(rs.getFloat("money"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return orders;
    }

    @Override
    public int modifyStatus(int orderId, String status, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {status, orderId};
        String sql = "update `order` set status = ? where order_id = ?";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        if (excute > 0 && "完成".equals(status)) {
            Object[] objects = {storeId};
            String s = "update store set unprocess_order_num = unprocess_order_num-1,process_order_num=process_order_num+1 where store_id = ?";
            JdbcUtils.excute(connection, pstm, s, objects);
        }
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public List<Order> getOwnOrder(int userId) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {userId};
        List<Order> orders = new ArrayList<>();
        String sql = "select o.order_id,status,money,d.dish_name,s.store_name from `order` o " +
                "inner join dish d on d.dish_id = o.dish_id " +
                "inner join store s on s.store_id = o.store_id " +
                "where o.user_id = ?";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        try {
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setStatus(rs.getString("status"));
                order.setMoney(rs.getFloat("money"));
                order.setDishName(rs.getString("dish_name"));
                order.setStoreName(rs.getString("store_name"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return orders;
    }
}
